const {
	Service
} = require('uni-cloud-router')
const {empty , typeOf} = require('../methods/index.js')

module.exports = class teachingClassService extends Service {
	constructor(ctx) {
		super(ctx)
		this.dbCmd = this.db.command
		this.user = this.db.collection('uni-id-users')
		this.hrCourse = this.db.collection('hr-course')
		this.hrClassroom = this.db.collection('hr-classroom')
		this.classroomSchedule = this.db.collection('classroomSchedule')
		const date = new Date()
		this.year = date.getFullYear()
		this.term = date.getMonth() + 1 < 7 ? 0 : 1 
	}
	
	/**
	 * 搜索课程
	 */
	async searchCourse(){
		return await this.hrCourse.where({
			year:this.year,
			term:this.term
		}).get().then((res)=>{
			let list = []
			return list = res.data.map((item)=>{
				let {course_id , course_name} = item
				 return { value: course_id, label: `${course_id}-${course_name}` };
			})
		})
	}
	
	/*
	* 搜索教师 
	*/
	async searchTeacher(){
		return await this.user.where({
			['role']:'TEACHER'
		}).get().then((res)=>{
			let list = []
			return list = res.data.map((item)=>{
				let {username , nickname} = item
				 return { value: username, label: `${username}-${nickname}` };
			})
		})
	}
	
	/**
	 * 筛选教室
	 * @param {Number} day 周几
	 * @param {Number} node 第几节 
	 * @param {String} chooseRoom 已选的课室号(非必填) 
	 */
	async screenRoom(day , node , chooseRoom){
		return await this.hrClassroom.get().then(async (res)=>{
			let classroomList = res.data
			let resultList = []
			return await this.classroomSchedule.where({day: day+'',node: node+''}).field({room_name:true}).get().then((res)=>{
				let {data} = res
				if(!empty(chooseRoom)){
					data = data.filter((room)=>room.room_name !== chooseRoom)
				}
				console.log(data)
				let filterRoom = []
				data.map((item)=> filterRoom.push(item.room_name))
				if(res.affectedDocs === 0 || empty(data)){
					return classroomList
				}
				return new Promise((resolve,reject)=>{
						 resultList = classroomList.filter((room)=>{
							 if (!filterRoom.includes(room.room_name)) return room;
						 })
						 resolve(resultList)
				})
			})
		})
	}
	
	async packageRoom(roomList){
		let classRoomOptions = [{
			value: '0',label: '书写墙教室',children: []},{
			value: '1',label: '实验室',children: []},{
			value: '2',label: '室内运动场',children: []},{
			value: '3',label: '室外运动场',children: []},{
			value: '4',label: '美术教室',children: []},{
			value: '5',label: '音乐教室',children: []}]
			console.log(roomList)
		return new Promise((resolve,reject)=>{
			roomList.map((item,index)=>{
				classRoomOptions.some((key)=>{
					if(item.type == key.value){
						if(key.children.length == 0){
							key.children.push({
								value: item.building,
								label: item.building,
								children: [{
									value: item.room_name,
									label: item.room_name
								}]
							})
							return
						}
						if(key.children.length != 0){
							key.children.some((child)=>{
								if(item.building == child.value && item.room_name != child.children.value){
									child.children.push({
										value: item.room_name,
										label: item.room_name
									})
									return true
								}else{
									key.children.push({
										value: item.building,
										label: item.building,
										children: [{
											value: item.room_name,
											label: item.room_name
										}]
									})
								}
							})
						}
					}
				})
				if(index === roomList.length - 1){
					resolve(classRoomOptions)
				}
			})
		})
	}
	
	/**
	 * 搜索空闲教室 -> 筛选教室 -> 封装教室数据
	 * @param {Number} day 周几
	 * @param {Number} node 第几节 
	 * @param {String} chooseRoom 已选的课室号(非必填) 
	 */
	async searchRoom(day , node , chooseRoom){
		return await this.service.teachingClass.screenRoom(day , node , chooseRoom).then(async (res)=>{
			console.log(res)
			return await this.service.teachingClass.packageRoom(res)
		})
	}
}